package com.example.weather_forecast;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class WeatherAlertManager {

    // 共享偏好设置名称和键名
    private static final String PREFS_NAME = "WeatherAlertSettings";
    private static final String KEY_ALERT_RAIN = "alert_rain";
    private static final String KEY_ALERT_TYPHOON = "alert_typhoon";
    private static final String KEY_ALERT_HEAT = "alert_heat";
    private static final String KEY_ALERT_COLD = "alert_cold";
    private static final String KEY_SOUND = "sound";
    private static final String KEY_VIBRATION = "vibration";
    private static final String KEY_ENABLED = "enabled";

    /**
     * 设置天气预警的配置
     *
     * @param context         应用上下文
     * @param enableRain      是否启用暴雨预警
     * @param enableTyphoon   是否启用台风预警
     * @param enableHeat      是否启用高温预警
     * @param enableCold      是否启用寒潮预警
     * @param enableSound     是否启用声音提醒
     * @param enableVibration 是否启用振动提醒
     */
    public static void setWeatherAlertSettings(Context context, boolean enableRain, boolean enableTyphoon,
                                               boolean enableHeat, boolean enableCold,
                                               boolean enableSound, boolean enableVibration) {
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putBoolean(KEY_ENABLED, true);
        editor.putBoolean(KEY_ALERT_RAIN, enableRain);
        editor.putBoolean(KEY_ALERT_TYPHOON, enableTyphoon);
        editor.putBoolean(KEY_ALERT_HEAT, enableHeat);
        editor.putBoolean(KEY_ALERT_COLD, enableCold);
        editor.putBoolean(KEY_SOUND, enableSound);
        editor.putBoolean(KEY_VIBRATION, enableVibration);

        editor.apply();
    }

    /**
     * 禁用所有天气预警
     *
     * @param context 应用上下文
     */
    public static void disableAllAlerts(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putBoolean(KEY_ENABLED, false);
        editor.apply();
    }

    /**
     * 检查指定类型的预警是否已启用
     *
     * @param context   应用上下文
     * @param alertType 预警类型 ("rain", "typhoon", "heat", "cold")
     * @return 如果预警类型已启用则返回true，否则返回false
     */
    public static boolean isAlertEnabled(Context context, String alertType) {
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

        // 首先检查总开关是否启用
        if (!preferences.getBoolean(KEY_ENABLED, false)) {
            return false;
        }

        // 然后检查特定预警类型是否启用
        switch (alertType) {
            case "rain":
                return preferences.getBoolean(KEY_ALERT_RAIN, false);
            case "typhoon":
                return preferences.getBoolean(KEY_ALERT_TYPHOON, false);
            case "heat":
                return preferences.getBoolean(KEY_ALERT_HEAT, false);
            case "cold":
                return preferences.getBoolean(KEY_ALERT_COLD, false);
            default:
                return false;
        }
    }

    /**
     * 发送天气预警通知
     *
     * @param context   应用上下文
     * @param alertType 预警类型 ("rain", "typhoon", "heat", "cold")
     */
    public static void sendWeatherAlert(Context context, String alertType) {
        // 检查该类型的预警是否已启用
        if (!isAlertEnabled(context, alertType)) {
            return;
        }

        // 获取声音和振动设置
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        boolean enableSound = preferences.getBoolean(KEY_SOUND, true);
        boolean enableVibration = preferences.getBoolean(KEY_VIBRATION, true);

        // 创建并发送预警通知
        Intent intent = new Intent(context, NotificationReceiver.class);
        intent.setAction(NotificationReceiver.ACTION_WEATHER_ALERT);
        intent.putExtra(NotificationReceiver.EXTRA_ALERT_TYPE, alertType);
        intent.putExtra(NotificationReceiver.EXTRA_SOUND, enableSound);
        intent.putExtra(NotificationReceiver.EXTRA_VIBRATION, enableVibration);

        context.sendBroadcast(intent);
    }

    /**
     * 这个方法将在应用启动时或者从天气API接收到新数据时调用，
     * 用于检查是否需要发送预警通知
     *
     * @param context 应用上下文
     * @param weatherData 天气数据对象，包含各种天气指标
     */
    public static void checkWeatherAlerts(Context context, WeatherData weatherData) {
        // 如果总开关未启用，则不处理
        SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        if (!preferences.getBoolean(KEY_ENABLED, false)) {
            return;
        }

        // 检查各类预警条件
        // 注意：实际应用中，这些阈值应该根据当地气象部门的标准来设定

        // 暴雨预警（假设rainfall是毫米/小时）
        if (preferences.getBoolean(KEY_ALERT_RAIN, false) && weatherData.getRainfall() >= 20.0) {
            sendWeatherAlert(context, "rain");
        }

        // 台风预警（假设windSpeed是米/秒）
        if (preferences.getBoolean(KEY_ALERT_TYPHOON, false) && weatherData.getWindSpeed() >= 17.0) {
            sendWeatherAlert(context, "typhoon");
        }

        // 高温预警（假设temperature是摄氏度）
        if (preferences.getBoolean(KEY_ALERT_HEAT, false) && weatherData.getTemperature() >= 35.0) {
            sendWeatherAlert(context, "heat");
        }

        // 寒潮预警（假设temperature是摄氏度且24小时内温度下降超过8度）
        if (preferences.getBoolean(KEY_ALERT_COLD, false) &&
                weatherData.getTemperature() <= 10.0 &&
                weatherData.getTemperatureDrop24h() >= 8.0) {
            sendWeatherAlert(context, "cold");
        }
    }
}
